package com.cy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@RequestMapping("/consumer/")
public class RestConsumerController {

    @Autowired
    private RestTemplate LoadBalancedRestTemplate;

//    @PutMapping
//    public Map<String, Object> doUpdate(@RequestBody Map<String, Object> request) {
//        System.out.println("consumer.requst=" + request);
//        String url = String.format("http://%s/provider/", "sca-provider");
//        ResponseEntity<Map> ResponseEntity = LoadBalancedRestTemplate.exchange(
//                url,
//                HttpMethod.PUT,
//                new HttpEntity<>(request),
//                Map.class);
//        return ResponseEntity.getBody();
//    }

    @PostMapping
    public Map<String,Object> doCreate(Map<String,Object> request){
        System.out.println("consumer.requst=" + request);
        String url=String.format("http://%s/provider/","sca-provider");
        return LoadBalancedRestTemplate.postForObject(
                url,
                request,
                Map.class);
    }


    @DeleteMapping("{id}")
    public String doDeleteById(@PathVariable Long id) {
        String url = String.format("http://%s/provider/%s", "sca-provider", id);
        ResponseEntity<String> exchange = LoadBalancedRestTemplate.exchange(
                url,
                HttpMethod.DELETE,
                null,
                String.class);
        return exchange.getBody();
    }
}
